package me.guitar.restweb.util;


import org.nutz.mvc.Mvcs;

import javax.servlet.http.Cookie;

public final class CookieUtil {
    private CookieUtil() {
    }
    /**
     * 添加cookie
     *
     * @param name
     * @param value
     * @param maxAge
     */
    public static void addCookie(String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        if (maxAge > 0) {
            cookie.setMaxAge(maxAge);
        }
        Mvcs.getResp().addCookie(cookie);
    }
    
    /**
     * 删除cookie
     *
     * @param name
     */
    public static void removeCookie(String name) {
        Cookie cookie = new Cookie(name, null);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        Mvcs.getResp().addCookie(cookie);
    }
    
    /**
     * 获取cookie值
     *
     * @return
     */
    public static String getUid(String cookieName) {
        Cookie cookies[] = Mvcs.getReq().getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(cookieName)) {
                return cookie.getValue();
            }
        }
        return null;
    }
}
